<template>
	<view class="filter-page">
		<view class="status_bar"></view>
		<view class="top">
			<u-icon name="arrow-left" size="42rpx" color='#fff' @click="back"></u-icon>
		</view>
		<view class="main">
			<!-- 账户安全 -->
			<view class="cell" @click="toAccount">
				<view class="label">账户安全</view>
				<u-icon name="arrow-right" size="30rpx" color='#fff'></u-icon>
			</view>
			<u-line></u-line>
			<!-- 内存清理 -->
			<view class="cell" @click="clearCacheConfirm" v-if="false">
				<view class="label">内存清理</view>
				<view class="right">
					<view class="content">{{cacheSize}}</view>
					<u-icon name="arrow-right" size="30rpx" color='#fff'></u-icon>
				</view>
			</view>
			<!-- <u-line></u-line> -->
			<!-- 用户协议 -->
			<view class="cell" @click="goAgreement" v-if="false">
				<view class="label">用户协议</view>
				<u-icon name="arrow-right" size="30rpx" color='#fff'></u-icon>
			</view>
			<!-- <u-line></u-line> -->
			<!-- 隐私政策 -->
			<view class="cell" @click="goPrivacy" v-if="false">
				<view class="label">隐私政策</view>
				<u-icon name="arrow-right" size="30rpx" color='#fff'></u-icon>
			</view>
			<!-- <u-line></u-line> -->
			<!-- 个性化推送 -->
			<view class="cell" v-if="false">
				<view class="label">个性化推送</view>
				<view class="right">
					<switch :checked="checked" @change="pushChange" />
				</view>
			</view>
			<!-- <u-line></u-line> -->
			<!-- 版本号 -->
			<view class="cell">
				<view class="label">版本号</view>
				<view class="right">
					<view class="content">V{{version}}</view>
				</view>
			</view>
			<u-line></u-line>

			<view class="btn" @click="logout">
				退出登录
			</view>
		</view>
	</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				cacheSize: '',
				version: '',
				checked: false
			}
		},
		onShow(option) {
			this.getInfo();
		},
		methods: {
			back() {
				uni.navigateBack()
			},
			toAccount() {
				uni.navigateTo({
					url: '/pages/m-pages/m-settle-account/index'
				})
			},
			clearCacheConfirm() {
				uni.showModal({
					title: "操作确认",
					content: "确认要清理缓存吗?",
					success: (res) => {
						if (res.confirm) {
							this.clearCache()
						}
					}
				})
			},
			goAgreement() {
				getApp().globalData.webviewUrl = 'https://www.xksndf2002.cn/agreement-lalaq.html'
				uni.navigateTo({
					url: '/pages/webview/webview'
				})
			},
			goPrivacy() {
				getApp().globalData.webviewUrl = 'https://www.xksndf2002.cn/privacy-lalaq.html'
				uni.navigateTo({
					url: '/pages/webview/webview'
				})
			},
			pushChange(e) {
				uni.setStorageSync("push", e.detail.value);
			},
			logout() {
				try{
					uni.$TUIKit.logout();
				}catch(e){
				}
				uni.reLaunch({
					url: '/im/login/login',
					success() {
						uni.removeStorageSync('userInfo');
						uni.removeStorageSync('token')
						uni.showToast({
							title:'退出成功',
							icon: 'none'
						})
					}
				});
			},
			//清除缓存
			clearCache() {
				let that = this;
				// #ifdef APP-PLUS
				let os = plus.os.name;
				if (os == 'Android') {
					let main = plus.android.runtimeMainActivity();
					let sdRoot = main.getCacheDir();
					let files = plus.android.invoke(sdRoot, "listFiles");
					let len = files.length;
					for (let i = 0; i < len; i++) {
						let filePath = '' + files[i]; // 没有找到合适的方法获取路径，这样写可以转成文件路径  
						plus.io.resolveLocalFileSystemURL(filePath, function(entry) {
							if (entry.isDirectory) {
								entry.removeRecursively(function(entry) { //递归删除其下的所有文件及子目录  	
									that.getAppInfo(); // 重新计算缓存
								}, function(e) {
									console.log(e.message)
								});
							} else {
								entry.remove();
							}
						}, function(e) {
							console.log('文件路径读取失败')
						});
					}
					uni.showToast({
						title: '缓存清理完成',
						duration: 2000
					});
				} else { // ios  
					plus.cache.clear(function() {
						uni.showToast({
							title: '缓存清理完成',
							duration: 2000
						});
						that.getAppInfo();
					});
				}
				// #endif
			},
			async getInfo() {
				// #ifdef APP-PLUS
				//a.版本号
				plus.runtime.getProperty(plus.runtime.appid, (info) => {
					this.version = info.version;
				});
				//a.缓存
				plus.cache.calculate(size => {
					if (size < 1024) {
						this.cacheSize = size + 'B';
					} else if (size / 1024 >= 1 && size / 1024 / 1024 < 1) {
						this.cacheSize = Math.floor((size / 1024) * 100) / 100 + 'KB';
					} else if (size / 1024 / 1024 >= 1) {
						this.cacheSize = Math.floor((size / 1024 / 1024) * 100) / 100 + 'M';
					}
				});
				// #endif
				//c.个性化推送
				if (uni.getStorageSync("push")) {
					this.checked = uni.getStorageSync("push");
				}
			},
		}
	}
</script>

<style scoped lang="scss">
	.status_bar {
		height: var(--status-bar-height);
		width: 100%;
	}

	.top {
		padding: 20rpx 30rpx;
	}

	.main {
		display: flex;
		flex-direction: column;
		padding: 0 30rpx;

		.cell {
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: space-between;
			height: 100rpx;

			.label {
				font-size: 28rpx;
				color: #fff;
			}

			.right {
				display: flex;
				flex-direction: row;
				align-items: center;

				.content {
					font-size: 24rpx;
					color: #fff;
				}
			}
		}

		.btn {
			margin-top: 100rpx;
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			font-size: 28rpx;
			color: #fff;
			background-color: #F49C66;
			border-radius: 40rpx;
		}
	}
</style>